package com.hqj.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqj.client.entity.BlogsLikes;
import com.hqj.client.mapper.BlogsLikesMapper;
import com.hqj.client.service.BlogsLikesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.file.Watchable;
import java.util.List;

/**
 * @author 王建林
 * @version 1.0
 * @date 2020/10/15
 */
@Service
public class BlogsLikesServiceImpl implements BlogsLikesService {
    @Resource
    private BlogsLikesMapper blogsLikesMapper;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return java.util.List<com.hqj.client.entity.BlogsLikes>
     * @date 2020/10/15 15:47
     */
    @Override
    public List<BlogsLikes> findPage(int page, int size) {
        Page<BlogsLikes> likesPage = blogsLikesMapper.selectPage(new Page<>(page, size), null);
        return likesPage.getRecords();
    }

    /**
     * 增加
     *
     * @param blogsLikes
     * @return int
     * @date 2020/10/15 16:31
     */
    @Override
    public int add(BlogsLikes blogsLikes) {
        QueryWrapper<BlogsLikes>wrapper=new QueryWrapper<>();
        wrapper.eq("username",blogsLikes.getUsername()).eq("blogs_id",blogsLikes.getBlogsId());
        BlogsLikes blogsLikes1 = blogsLikesMapper.selectOne(wrapper);
        if(blogsLikes1==null){
            blogsLikes.setIsDetele(0);
            return blogsLikesMapper.insert(blogsLikes);
        }else {
            blogsLikes1.setIsDetele(blogsLikes1.getIsDetele()==0?1:0);
            return blogsLikesMapper.updateById(blogsLikes1);
        }
    }

    /**
     * 修改
     *
     * @param blogsLikes
     * @return int
     * @date 2020/10/15 16:31
     */
    @Override
    public int updates(BlogsLikes blogsLikes) {
        return blogsLikesMapper.updateById(blogsLikes);
    }

    /**
     * 删除
     *
     * @param id 博客id
     * @return int
     * @date 2020/10/15 16:43
     */
    @Override
    public int delete(Long id) {
        QueryWrapper<BlogsLikes> wrapper = new QueryWrapper<>();
        wrapper.eq("blogs_id", id);
        BlogsLikes blogsLikes = blogsLikesMapper.selectOne(wrapper);
        blogsLikes.setIsDetele(1);
        return blogsLikesMapper.updateById(blogsLikes);
    }
    /**
     * 判断是否点赞
     * @date 2020/10/29 15:48
     * @param blogsId
     * @param username
     * @return com.hqj.client.entity.BlogsLikes
     */
    @Override
    public BlogsLikes judgelikes(Long blogsId, String username) {
        QueryWrapper<BlogsLikes>wrapper=new QueryWrapper<>();
        wrapper.eq("blogs_id",blogsId).eq("username",username);
        BlogsLikes blogsLikes = blogsLikesMapper.selectOne(wrapper);
        return blogsLikes;
    }


}
